package com.ifcar.app.activity;

import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.util.Tools;
import com.ifcar.app.activity.R;

import android.R.anim;
import android.app.Activity;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class AccountWebViewRegister extends BaseActivity {
    
    private WebView registerWebview;
    private LinearLayout loadingbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.accountwebviewregister);
        findViewsById();
        registerWebview.getSettings().setJavaScriptEnabled(true);
        registerWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//        if (android.os.Build.VERSION.SDK_INT >= 5) {
//            registerWebview.getSettings().setDatabaseEnabled(false);
//        }
        registerWebview.getSettings().setSaveFormData(false);
        registerWebview.getSettings().setSavePassword(false);
        registerWebview.clearCache(true);
        
        String host = Tools.getHostbyWAP(this);
        if(null!=host){
            registerWebview.enablePlatformNotifications();
            registerWebview.setHttpAuthUsernamePassword(host,"80","","");
        }
        setListener();
        
        new Thread() {
            public void run() {
                registerWebview.loadUrl("http://reg.163.com/reg/reg_mob2.jsp?product=newsclient");
            }
        }.start();
    }
    

    private void findViewsById() {
       registerWebview = (WebView)findViewById(R.id.registerWebview);
       loadingbar = (LinearLayout) findViewById(R.id.weibologin_loadingbar);
       loadingbar.setVisibility(View.VISIBLE);
    }
    
    private void setListener(){
       
        registerWebview.setWebViewClient(new WebViewClient(){
            
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
              
                loadingbar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
                
                if(null!=url && url.contains("username=")){
                    String username = Tools.parseURL(url, "username");
                    if(!"".equals(username)){
                        Intent i = new Intent(AccountWebViewRegister.this, AccountLogin.class);
                        
                        Bundle b = new Bundle();
                        b.putString("username", username);
                        b.putBoolean("isRegister", true);
                        i.putExtras(b);
                        
                        startActivity(i);
                        
                        AccountWebViewRegister.this.finish();
                        registerWebview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                    }
                   
                }
                
            }
            

            public void onReceivedSslError(WebView view,
                    SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                try {
                    if(handler != null)
                    handler.proceed();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
             
                return true;
            }
            
        });
    }

    public void goBackClick(View view){
		finish();
	}

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        String host = Tools.getHostbyWAP(this);
        if(null!=host){
            if(null!=registerWebview){
                registerWebview.disablePlatformNotifications();
            }
        }
        
    }

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        themeSettingsHelper.setViewBackgroud(this, R.id.top_space, R.drawable.top_background);
        themeSettingsHelper.setTextViewColor(this, R.id.top_txt, R.color.main_title_color);
      //返回夜间模式
        themeSettingsHelper.setImageViewSrc(this, R.id.go_back_common_btn, R.drawable.go_back);
        
    }
}
